use std::io::{ErrorKind, Read, Write};
use std::net::TcpListener;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

const MSG_SIZE: usize = 1024;
const LOCAL_SERVER: &str = "127.0.0.1:8888";

fn main() {
  let server = TcpListener::bind(LOCAL_SERVER).expect("服务监听失败");

  server
    .set_nonblocking(true)
    .expect("设置 non-blocking 失败");

  let mut clients = vec![];

  let (tx, rx) = mpsc::channel::<String>();

  loop {
    if let Ok((mut socket, addr)) = server.accept() {
      println!("{} connected", addr);
      clients.push(socket.try_clone().expect("failed to clone client"));
      let tx = tx.clone();
      thread::spawn(move || loop {
        let mut buff = vec![0; MSG_SIZE];
        match socket.read_exact(&mut buff) {
          Ok(_) => {
            let msg =
              buff.into_iter().take_while(|&x| x != 0).collect::<Vec<_>>();
            let msg_string = String::from_utf8(msg).expect("无效的字符");
            println!("{}: {:?}", addr, msg_string);

            tx.send(msg_string).expect("发送消失失败");
          }
          Err(ref err) if err.kind() == ErrorKind::WouldBlock => (),
          Err(_) => {
            println!("关闭连接 {}", addr);
            break;
          }
        }

        sleep();
      });
    }

    if let Ok(msg) = rx.try_recv() {
      clients = clients
        .into_iter()
        .filter_map(|mut client| {
          let mut buff = msg.clone().into_bytes();
          buff.resize(MSG_SIZE, 0);
          client.write_all(&buff).map(|_| client).ok()
        })
        .collect::<Vec<_>>();
    }
    sleep();
  }
}

fn sleep() {
  thread::sleep(Duration::from_millis(100));
}
